<?php
class MY_Model extends CI_Model
{
    public function __construct()
    {
        parent::__construct();
        $this->rate = 60;  //达标成绩
    }

    public function trans_start()
    {
        $this->db->trans_start();
    }

    public function trans_complete()
    {
        return $this->db->trans_complete();
    }

    public function get_cat_list()
    {
        $this->db->order_by("sort", "desc");
        $query = $this->db->get("tr_department");
        return $query->result_array();
    }

    protected function _geQuerySql($sql)
    {
        $query = $this->db->query($sql);
        $result = $query->result_array();
        return $result;
    }
    protected function _operDbSql($sql){
        $query = $this->db->query($sql);
        return $query;
    }
    public function insertData($table,$params){
        $query = $this->db->insert($table,$params);
        return $this->db->insert_id();
    }

    protected function spliceArrByPage($page, $num, $array)
    {
        $resultArr = array_slice($array, ($page * $num), $num);
        return $resultArr;
    }

    public function getQuestionInfo($type, $paperID)
    {
        $questionInfo = array('paperInfo' => array(), 'questionInfo' => array());
        $table_paper = ($type == 1) ? 'tr_test_paper' : 'tr_examination_paper';
        $table_question = ($type == 1) ? 'tr_test_question' : 'tr_examination_question';
        $table_option = 'tr_question_option';
        $title_sql = 'select paperTitle,paperID from ' . $table_paper . " where paperID=" . $paperID;
        $paper = $this->_geQuerySql($title_sql);
        $questionInfo['paperInfo']['paperTitle'] = !empty($paper) ? $paper[0]['paperTitle'] : ''; //问卷标题
        $questionInfo['paperInfo']['paperID'] = !empty($paper) ? $paper[0]['paperID'] : ''; //问卷标题
        $question_sql = 'select question.*,q_option.optionTitle from ' . $table_question . ' as question left join ' . $table_option . ' as q_option on question.questionID=q_option.questionID where question.paperID=' . $paperID;
        $question = $this->_geQuerySql($question_sql); //获取question信息
        $questionInfo['questionInfo'] = $this->arrayMosaic($question);
        return $questionInfo;

    }

    public function arrayMosaic($question){
        $question_tmp = array();
        $option_tmp = array();
        if (!empty($question)) {
            foreach ($question as $key => $value) { //过滤拼接选项
                $question_tmp[$value['questionID']] = $value;
                if ($value['optionTitle']) {
                    $option_tmp[$value['questionID']][] = $value['optionTitle'];
                    $question_tmp[$value['questionID']]['option'] = $option_tmp[$value['questionID']];
                }
                unset($question_tmp[$value['questionID']]['optionTitle']);
            }
        }
        return  $question_tmp;
    }

    /*
     * 计算答题得分 edit by cc at 20170116
     */
    public function getScoreByAnswer($answer,$questionAnser){
        $correct = 0;
        $weight = 10;
        if(empty($questionAnser) || empty($answer)) return $correct;
        $questionAnser = $this->switchArrayKeys('questionID',$questionAnser);
        foreach($questionAnser as $key=>$value){
            if(in_array($value['questionType'],array(1,2,3))){
                if(!is_array($answer[$key])) $answer[$key] = json_decode($answer[$key],true);
                if($answer[$key]==json_decode($value['answer'],true)){
                    $correct++;
                    continue;
                }else{
                    //记录打错的题进入我的错题
                    $departmentid = $_SESSION['userInfo']['departmentid'];
                    $accountID = $_SESSION['userInfo']['AccountID'];
                    $sql = "replace into tr_wrong_question(questionID,answer,userID,departmentID,createTime) values(".$key.",".json_encode($value['answer']).",".$accountID.",".$departmentid.",now())";
                    $this->_operDbSql($sql);
                    continue;
                }
            }
        }
        return $correct*$weight;
    }

    /*
     * 更换数组key值
     */
    protected function switchArrayKeys($fields,$array){
        $tmpArray = array();
        foreach($array as $key=>$value){
            $tmpArray[$value[$fields]] = $value;
        }
        return $tmpArray;
    }
    /*
     * 根据考试ID、部门、时间获取成绩
     */
    public function getStandard($params){
        $where = " where 1 ";
        if(isset($params['examinationID'])){
            $where .= " and examinationID in (".$params['examinationID'].")";
        }
        if(isset($params['departmentID'])){
            $where .= " and departmentID in (".$params['departmentID'].")";
        }
        if(isset($params['startTime'])){
            $where .= " and createTime >= '".$params['startTime']."'";
        }
        if(isset($params['endTime'])){
            $where .= " and createTime <= '".$params['endTime']."'";
        }
        if(isset($params['userID'])){
            $where .= " and userID in (".$params['userID'].")";
        }
        $sql = "select * from tr_user_score ".$where;
        $result = $this->_geQuerySql($sql);
        return $result;
    }
    /*
     * 计算达标率
     */
    public function getStandRate($arr){

        $tmp = array('all'=>0,'correct'=>0,'wrong'=>0,'rate'=>"0%");
        if(is_array($arr) && !empty($arr)){
            $count = count($arr);
            $correct = 0;
            foreach($arr as $key=>$value){
                if($value['score']>=$this->rate){
                    $correct ++;
                    continue;
                }
            }
            $wrong = $count - $correct;
            $rate = $count>0 ? (($correct/$count)*100)."%" : "0%";
            $tmp['all'] = $count;
            $tmp['correct'] = $correct;
            $tmp['wrong'] = $wrong;
            $tmp['rate'] = $rate;
            $tmp['rateTrue'] = ($correct/$count);
        }
        return $tmp;

    }

    /*
     * 定义数组随机分组规则
     * 用于确定一次考试生成几份试卷
     * @params num代表参加考试人数
     */
    public function randArrayByNum($num){
        if($num<=5) return 1;
        if(5<$num &&$num<=10) return 2;
        if($num<=30 && $num>10) return 3;
        if($num<=50 && $num>30 ) return 4;
        if($num>50 && $num < 80) return 6;
        if($num>=80) return 10;
    }
    /*
     * 数组分组
     * 用于分批考试人员列表
     */
    public function getRouterNum($arr){
        $count = count($arr);
        $num = $this->randArrayByNum($count);
        $num = ceil(($count/$num));
        return array_chunk($arr,$num);
    }

}
?>